<?php

class FNews extends NewsletterContent {
    
	public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function getCategory($typeId='') {
        //criteria
        $criteria = new CDbCriteria;
        $criteria->compare('activated_flag', true);
        
        if ($typeId != '' && is_numeric($typeId))
            $criteria->compare('type_id', $typeId);
        
        //sort
        $sort = new CSort();
        $sort->defaultOrder = 'sort_order DESC';
        
        //pagination
        $page = new CPagination();
        $page->pageSize = Yii::app()->params['front_page_size'];
        
        return new CActiveDataProvider($this, array(
            'criteria'   => $criteria,
            'sort'       => $sort,
            'pagination' => $page,
        ));
    }
    
    /**
     * @param string $titleSeo
     * @return array With format array('title' => 'abc', 'type_id' => '11')
     */
    public function getType($titleSeo='') {
        if (! empty($titleSeo)) {
            $model = new NewsletterType();
            $data = $model->findAllByAttributes(array(
                'activated_flag' => true,
            ));
            $list = array();

            foreach ($data as $row) {
                $list[$row->title_seo] = array(
                    'title'   => $row->title,
                    'type_id' => $row->id,
                );
            }

            return (isset($list[$titleSeo])) ? $list[$titleSeo] : null;
        } else {
            return null;
        }
    }
    
    /**
     * Get content from [newsletter_content]
     * @param mixed $typeId array or integer
     * @param integer $limit
     * @param array $data data of a record
     * @return array 
     */
    public function getTop($typeId, $limit='', $data='') {
        $model    = new NewsletterContent;
        $criteria = new CDbCriteria();
        if (empty($limit)) $limit = 10;

        $criteria->compare('activated_flag', true);
        
        if (is_numeric($typeId)) {
            $criteria->compare('type_id', $typeId);
        } elseif (is_array($typeId)) {
            $maxRepeat = count($typeId);
            for ($i=0; $i<$maxRepeat; $i++) {
                 if ($i != ($maxRepeat - 1)) {
                    $criteria->compare('type_id', $typeId, false, 'OR');
                } else {
                    $criteria->compare('type_id', $typeId);
                }
            }
        }

        if ($data) $criteria->compare ('sort_order', '<' . $data['sort_order']);

        $criteria->order = 'sort_order DESC';
        $criteria->limit = $limit;
        
        return $model->findAll($criteria);
    }
    
    public function getDetail($var, $activatedFlag=true) {
        $aVar = explode('-', $var);
        if (isset($aVar['1']) && ($aVar['1']=='pvw')) //preview
            $activatedFlag = '';

        if (isset($aVar['0']) && is_numeric($aVar['0'])) {
            $criteria = new CDbCriteria();
            $criteria->compare('activated_flag', $activatedFlag);

            return NewsletterContent::model()->findByPk($aVar['0'], $criteria);
        }
        else
            return null;
    }
    
    public function getTypeParent($typeId='') {
        $result = null;
        
        if ( ! empty($typeId) ) {
            $model = new NewsletterType();
            $data = $model->findByPk($typeId);
            if ( $data ) {
                $result = $data->parent_id;
            }
        }
        return $result;
    }
    
}
